#!/sbin/sh
#
# Create an HTML index from the installed Freeware products based
# on the release notes.
#
# usage: $0 [-installed|-web]
# -installed 	create index.html and index for installed products.
# -web			create web pages (i.e. leave out installed.html)
# -web is designed for internal SGI use only; generate pages for silicon surf.
#
# -installed is the default
#
# input: $COMMON/index.template
# output: $RELDIR/index.html, $RELDIR/installed.html
#

RELDIR=${rbase:=/}/usr/freeware/relnotes
COMMON=$RELDIR/shared  ## MUST NOT CHANGE. same location as old fw_common

INDEX=$RELDIR/index.html
INST=$RELDIR/installed.html
TMP=/tmp/mkindex.$$

INST_ED="INSTALLED INDEX"
INST_ABLE="INSTALLABLE INDEX"
HTTP_LINK="HTTP LINKS"


################
## subroutines

rminsted()
{ # rminsted $input_file
	# create index.html
	sed -e "/<!-- BEGIN $INST_ED -->/,/<!-- END $INST_ED -->/d" <$1 >$TMP
	mv -f $TMP $1
}

rminstable()
{ # rminstable $input_file
	# create index.html
	# remove INSTALLABLE portion if not available
	if [ ! -f $RELDIR/index-by-alpha.html ]
		then  ## fw_latest is not available
		sed -e "/<!-- BEGIN $INST_ABLE -->/,/<!-- END $INST_ABLE -->/d" <$1 >$TMP
		mv -f $TMP $1
	#else
		#nop
		fi
}

rmhttplink()
{ # rmhttplink $input_file
	# create index.html
	sed -e "/<!-- BEGIN $HTTP_LINK -->/,/<!-- END $HTTP_LINK -->/d" <$1 >$TMP
	mv -f $TMP $1
}

makeinstalled()
{ # makeinstalled $input_file
	cat >$1 <<_EOINDEX_
	<HTML>
	<HEAD>
	<TITLE>Installed products</TITLE>
	</HEAD>
	<BODY bgcolor=white>
	<H2>Product Listing</H2>
	<UL>
_EOINDEX_

	# Parse each release note and add a entry.  The entry will consist
	# of the title as represented in the <TITLE> identifier and a link
	# to the actual release note
	# first egrep -v is for files created by fw_common
	# second egrep -v is for files created by fw_latest
	# third egrep is to remove empty lines
	#
	# awk '{ for(i=1;i<=NF;i++){print $i;} }' = fmt -1
	for FILE in `cd $RELDIR && \
		ls -1 *.html | \
		egrep -v "(installed|index).html" | \
		egrep -v "index-(by-alpha|by-category|by-date|alt).html" | \
		egrep -v "^$" `
	do
		# sed -e 1q == head -1
		# sed -e 's/.*>\(.*\)>.*/\1/' == cut -f2- -d'>' | cut -f1 -d'<'
		#    to get the parts between <title>(I WANT THIS PART)</title>
		TITLE=`grep -i '<TITLE>' $RELDIR/$FILE | \
			sed -e '1q' | \
			sed -e 's/.*>\(.*\)<.*/\1/' `
		echo "<LI><A HREF=\"$FILE\">$TITLE</A>"		>>$1
	done
	
	# Create the footer information
	cat >>$1 <<_EOINDEX_
</UL>
	
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
	
</BODY>
</HTML>
_EOINDEX_
}

## subroutines
################

# parse command line option
# default to -installed
if [ "$1" = '' ] ; then MODE=-installed 
else MODE=$1 ; fi

# default is to remove all index.html link sections
RMINSTED=true
RMINSTABLE=true
RMHTTPLINK=true
# default is to create only the index.html file
INSTABLE=false
# set command line option
case $MODE in 
	-installed)  ## default
		RMINSTED=false
		INSTABLE=true
		RMHTTPLINK=false
		;;
	-web)
		echo "                                       WEB"
		RMINSTABLE=false
		;;
	esac
RMINSTED="[ true = $RMINSTED ]"
RMINSTABLE="[ true = $RMINSTABLE ]"
RMHTTPLINK="[ true = $RMHTTPLINK ]"
INSTABLE="[ true = $INSTABLE ]"
# init

# Remove the previous index if one exists
# Create the index.html ($INDEX)
rm -f $INDEX
cp $COMMON/index.template $TMP.start
$RMINSTED   && rminsted   $TMP.start
$RMINSTABLE && rminstable $TMP.start
$RMHTTPLINK && rmhttplink $TMP.start
mv -f $TMP.start $INDEX

# create installed.html ($INST)
$INSTABLE && makeinstalled $INST

exit 0
